package unit;

import hospital.Hospital;

/**
 * A class to create the specific unit policeCar
 */
public class PoliceCar extends Unit {

	/** Initialize this new policecar with a given name,speed and garage(homelocation)
	 * 
	 * @param	name
	 * 			The name for this policecar
	 * 
	 * @param 	location
	 * 			The location for this policecar
	 * 
	 * @param	garage
	 * 			The garage where this policecar has to be added to
	 * 
	 * @post 	The name of this new policecar is the same as the given name.
	 * 
	 * @post 	The location of this new policecar is the same as the given location.
	 * 
	 * @post	This policecar is added to the given garage
	 * */
	public PoliceCar(String name, int speed, Garage garage) {
		super(name, speed, garage);
	}

	/**
	 * Assign the hospital where to this policeCar has to drive
	 * 
	 * @throws IllegalArgumentException
	 *             You can't assign a hospital to a policeCar
	 */
	@Override
	public void assignHospital(Hospital h) {
		throw new IllegalArgumentException(
				"The chosen unit isn't an ambulance!");
	}

	/**
	 * Method for getting the total description of this PoliceCar
	 * 
	 * @return Returns the total description of this PoliceCar
	 */
	public String showInfo() {
		String output = "PoliceCar " + this.getName() + "\n";
		if (this.isAssigned()) {
			output += "current assignment: " + this.getEmergency().toString()
					+ "\n";
			if (this.getCurrentLocation() == this.getEmergency().getLocation()) {
				output += "unit is at emergency\n";
			} else {
				output += "unit isn't at emergency\n";
			}
		}
		output += "unit is waiting for an emergency\n";
		return output;
	}

	/**
	 * Return the type of unit, in this case policeCar
	 * 
	 * @return Return string representation of the type of this unit, 
	 * 			in this case policeCar and it's name
	 */
	@Override
	public String toString() {
		return "PoliceCar-" + getName();
	}
	

	/**
	 * Method for checking all the policecars are on the emergency ( else they can't do their task )
	 * 
	 * @return Returns true if all policecars are on the emergency, false if not 
	 * 
	 * @throws IllegalArgumentException, if this policecar doesn't has an emergency
	 */
	@Override
	public boolean unitsOfSameTypeOnEmergency() {
		boolean result = true;
		if (getEmergency() != null) {
			for (Unit u : getEmergency().getUnitsClone()) {
				if (u instanceof PoliceCar)
					if (!u.isAtEmergency())
						result = false;
			}
		}
		else{
			throw new IllegalArgumentException("This policecar doesn't has an emergency");
		}
		return result;
	}
}
